package main

import (
	"context"
	"fmt"
	"github.com/apache/thrift/lib/go/thrift"
	"phonebook/thrift/gen-go/thrift/generated"
)

func main() {
	// TCompactProtocol 压缩二进制
	protocolFactory := thrift.NewTCompactProtocolFactory()
	// transportFactory
	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())

	socket, err := thrift.NewTSocket("localhost:8899")
	if err != nil {
		panic(err)
	}

	transport, err := transportFactory.GetTransport(socket)
	client := generated.NewUserServiceClientFactory(transport, protocolFactory)
	if err := transport.Open(); err != nil {
		panic(err)
	}
	defer transport.Close()

	res, err := client.GetUserByUsername(context.Background(), "allen")

	if err != nil {
		panic(err)
	}

	fmt.Println("GetUserByUsername: " + res.String())

	err = client.SaveUser(context.Background(), &generated.User{generated.LongPtr(1), generated.StringPtr("allen"), generated.BooleanPtr(false), generated.IntPtr(40), nil, nil, nil})
	if err != nil {
		fmt.Println(err)
	}
}
